Understanding Pseudo-Elements in CSS
A pseudo-element in CSS allows you to style specific parts of an element without needing to add extra HTML markup. Common pseudo-elements include ::before, ::after, ::first-letter, and ::first-line.
Pseudo-elements let you style subparts of elements or insert content dynamically.
They are indicated by a double colon :: to distinguish them from pseudo-classes.
They do not exist in the DOM; they are virtual elements created for styling purposes.
Common use cases include decorative content, highlighting the first letter, or adding icons before/after text.
In this example, ::first-letter enlarges and colors the first letter, while ::before inserts a star before the paragraph text without modifying the HTML.
Use pseudo-elements to add stylistic content without cluttering HTML.
Combine with CSS properties like content, color, font-size, and background.
Avoid using them for interactive elements that require JavaScript functionality.
Test across browsers to ensure consistent rendering.
How would you add a star icon before every review rating using CSS without changing the HTML?
What happens if you try to apply a border to a ::before element without setting content: ''?
You’re trying to style a tooltip with ::after, but it’s not showing up—what are the first three things you’d check?
A designer wants a speech bubble tooltip using ::before and ::after, but it’s breaking on mobile—how would you debug and fix it?
Your team’s CSS library uses ::before for icons, but now accessibility audits say screen readers are reading the content—what’s the issue and how do you resolve it?
A button’s ::after hover effect works in Chrome but not Safari—what could be causing this, and how would you investigate?
You’re building a scalable component library with pseudo-elements for decorative elements—how do you balance performance, maintainability, and accessibility across 50+ components?
A legacy UI uses ::before for visual separators, but now we’re migrating to a CSS-in-JS system—what are the tradeoffs of keeping pseudo-elements vs. switching to divs or SVGs?
How would you design a responsive navigation indicator using pseudo-elements that works across browsers, supports dark mode, and doesn’t impact layout shift during hydration?
Your company’s design system relies heavily on pseudo-elements for UI embellishments—how would you architect a migration plan to reduce CSS complexity and improve render performance at scale?
Pseudo-elements are used across 20+ product teams for icons and indicators, but inconsistent usage is causing accessibility and testing issues—how do you enforce standards without stifling autonomy?
We’re considering removing all ::before/::after usage in favor of SVGs for better SSR and CDN caching—what are the long-term maintenance, performance, and developer experience tradeoffs you’d present to leadership?